<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"></script>
</head>
<body>
  <div id="app">
    <no-use-slot @set-username="uname = $event"></no-use-slot>
    <div>{{uname}}</div>
    <use-slot>
      <!-- vue2.6之前 -->
      <!-- <div slot-scope="{uname}">{{uname}}</div> -->
      <!-- vue2.6之后包括vue3 -->
      <template v-slot="{uname}">{{uname}}</template>
    </use-slot>
  </div>
</body>
<script>
  //作用域插槽
  //用途:子组件传数据给父组件
  //特征:
  //  子组件当中,在<slot></slot>上面通过v-bind/:绑定属性传数据
  //  父组件当中,通过slot-scope或者v-slot接受数据
  //优点:
  //  步骤更少,更简单
  //缺点
  //  默认只能在标签体里面用
  Vue.component('no-use-slot',{
    data(){
      return {
        username: 'zs'
      }
    },
    mounted() {
      this.$emit('set-username', this.username)
    },
    template: `
      <div>不用作用域插槽的子组件-</div>
    `
  })
  Vue.component('use-slot',{
    data(){
      return {
        username: 'zs'
      }
    },
    template: `
      <div>用作用域插槽的子组件-<slot :uname="username"></slot></div>
    `
  })
  new Vue({
    el: '#app',
    data() {
      return {
        uname: ''
      }
    },
    created() {},
    mounted() {
    },
    methods: {
    },
  })
</script>
</html>
